package com.xs.bookspoststation.BookActivity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.xs.bookspoststation.R;
import com.xs.bookspoststation.bean.Book;
import com.xs.bookspoststation.db.DBHelperBook;

public class UpdateBookActivity extends AppCompatActivity implements View.OnClickListener{
    //定义对象
    private EditText bookid,bookname,booknumber,bookauthor,bookshow;
    private Button submit,quit;
    //定义一个数据库帮助类
    private DBHelperBook dbHelperBook;
    //定义一个数据库操作类
    private SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_book);

        //操作对象的方法
        initView();

    }

    private void initView() {
        //绑定控件
        bookid = findViewById(R.id.edit_update_bookid1);
        bookname = findViewById(R.id.edit_update_bookname1);
        booknumber = findViewById(R.id.edit_update_booknumber1);
        bookauthor = findViewById(R.id.edit_update_bookauthor1);
        bookshow = findViewById(R.id.edit_update_bookshow1);
        submit = findViewById(R.id.btn_update_book2);
        quit = findViewById(R.id.btn_update_cancel2);
        //点击事件
        quit.setOnClickListener(this);
        submit.setOnClickListener(this);
        //实例化数据库帮助类对象
        dbHelperBook = new DBHelperBook(UpdateBookActivity.this,"book.db",null,1);
        //创建数据库并获取数据库的读写权限
        db = dbHelperBook.getWritableDatabase();
    }

    /**
     * 点击事件
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_update_cancel2:
                Intent intent=new Intent(UpdateBookActivity.this,BookViewActivity.class);
                Toast.makeText(getApplicationContext(), "返回上一界面", Toast.LENGTH_LONG).show();
                startActivity(intent);
                break;
            case R.id.btn_update_book2:
                goUpdate();
                break;
        }
    }
    /**
     * 界面启动执行此方法
     */
    @Override
    protected void onStart() {
        super.onStart();
        Intent intent=getIntent();
        //以下是获取查询图书信息界面传过来的数据
        String bookid1 = intent.getStringExtra("bookid");
        String bookname1 = intent.getStringExtra("bookname");
        String booknumber1 = intent.getStringExtra("booknumber");
        String bookanthor1 = intent.getStringExtra("bookauthor");
        String bookshow1 = intent.getStringExtra("bookshow");
        //将这些获取的数据进行显示
        bookid.setText(bookid1);
        //设置图书编号不能修改
        bookid.setEnabled(false);
        bookname.setText(bookname1);
        booknumber.setText(booknumber1);
        bookauthor.setText(bookanthor1);
        bookshow.setText(bookshow1);
        //打印日志
        Log.v("操作","！");
    }

    /**
     * 修改方法
     */
    private void goUpdate() {
        //如果点击了更新
        //还是判断一下是否填写完整，
        if(bookid.length()==0||bookname.length()==0||booknumber.length()==0||
                bookauthor.length()==0||bookshow.length()==0){
            Toast.makeText(UpdateBookActivity.this, "信息未填写完整！", Toast.LENGTH_LONG).show();
        }else{
            String wantUpdateBookid = bookid.getText().toString();
            String wantUpdateBookname = bookname.getText().toString();
            String wantUpdatebooknumber = booknumber.getText().toString();
            String wantUpdatebookauthor = bookauthor.getText().toString();
            String wantUpdatebookshow = bookshow.getText().toString();
            Book book = new Book(wantUpdateBookid,wantUpdateBookname,wantUpdatebooknumber,wantUpdatebookauthor,wantUpdatebookshow);
            //数据库操作
            SQLiteDatabase db = dbHelperBook.getWritableDatabase();//
            if(db.isOpen()){
                String bookid = book.getBookid();
                String bookname = book.getBookname();
                String booknumber = book.getBooknumber();
                String bookauthor = book.getBookauthor();
                String bookshow = book.getBookshow();
                //类似于HashMap，但这个只能存储基本类型
                ContentValues c = new ContentValues();
                c.put("bookid", bookid);
                c.put("bookname", bookname);
                c.put("booknumber", booknumber);
                c.put("bookauthor", bookauthor);
                c.put("bookshow", bookshow);
                db.update("bookInfo", c, "bookid=?",new String[]{bookid});
                Log.v("更新信息操作", "更新信息成功！");
            }else{
                Toast.makeText(UpdateBookActivity.this,"数据库打开失败",Toast.LENGTH_LONG).show();
            }
            Toast.makeText(UpdateBookActivity.this,"图书信息修改成功！",Toast.LENGTH_LONG).show();
            Intent intent2 =new Intent(UpdateBookActivity.this, BookViewActivity.class);
            startActivity(intent2);
        }

    }
}
